﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using ItemComponent;
using Player;
using Player.Fort;
using UI;

namespace Item
{
    public class ItemBase : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler,IPointerEnterHandler,IPointerExitHandler
    {
        protected List<IMyItemComponent> listComponent = new List<IMyItemComponent>();

        private string _itemTage;
        public virtual string ItemTag
        {
            get => _itemTage;
            set
            {
                _itemTage = value;

                strItemName = ItemDataMgr.Ins[ItemTag]?.strItemName;
                GetComponentInChildren<TextMesh>().text = strItemName;
            }
        }
        protected string strItemName;

        protected bool bFollowMouse = false;
        protected bool bShowItemDes = false;
        protected UIDesItem uiDesItem;
        protected System.Text.StringBuilder sb=new System.Text.StringBuilder();

        public FortNode CurNode { get; protected set; }

        protected virtual void Start()
        {
        }
        protected virtual void Update()
        {
            if (bFollowMouse)
            {
                FollowMouse();
                if (Input.GetMouseButtonUp(0))
                {
                    OnEndDrag(default(PointerEventData));
                    bFollowMouse = false;
                }
            }

            if (bShowItemDes)
            {
                RefreshDesUI();
            }
        }

        public void BeginFollowMouse()
        {
            gameObject.SetActive(true);
            bFollowMouse = true;
            FollowMouse();
        }
        protected void FollowMouse()
        {
            transform.position = Camera.main.ScreenToWorldPoint(Input.mousePosition) - Camera.main.transform.position.z * Vector3.forward;
        }

        public void OnBeginDrag(PointerEventData eventData)
        {
            OnCancelEquip();
        }

        public void OnDrag(PointerEventData eventData)
        {
            FollowMouse();
        }

        public void OnEndDrag(PointerEventData eventData)
        {
            var node = MyPlayer.Ins.MyFort.CheckCanEquipItem(transform.position, this);
            if (node != null)
            {
                if (!node.HaveItem)
                {
                    //可以去新点

                    //没有原节点，说明是背包新创建的，移动后需要从背包剔除
                    //有原节点，则原节点取消装备
                    if (CurNode == null)
                        MyPlayer.Ins.MyBackpack.RemoveItem(ItemTag);
                    else
                        CurNode.CancelEquip(this);

                    CurNode = node;
                    node.Equip(this);
                    OnEquip();
                }
                else if (CurNode != null)
                {
                    //回老点
                    CurNode.Equip(this);
                    OnEquip();
                }
                else
                    ReturnBackpack();//回背包
            }
            else
                ReturnBackpack();//回背包
        }

        public void RegistComponent(IMyItemComponent component)
        {
            if (component != null && !listComponent.Contains(component))
                listComponent.Add(component);
        }
        protected virtual void OnEquip()
        {
            listComponent?.ForEach((component) => component.Active = true);

            if (CurNode != null)
            {
                CurNode.EventOnBuffChange -= OnBuffChange;
                CurNode.EventOnBuffChange += OnBuffChange;

                OnBuffChange(CurNode.Buffs);
            }
        }
        protected virtual void OnCancelEquip()
        {
            listComponent?.ForEach((component) => component.Active = false);

            if (CurNode != null)
            {
                CurNode.EventOnBuffChange -= OnBuffChange;
            }
        }
        protected void ReturnBackpack()
        {
            if (CurNode != null)
            {
                //有阶段说明已经出过背包了，需要重新加回去
                CurNode.CancelEquip(this);
                //注销事件
                CurNode.EventOnBuffChange -= OnBuffChange;
                MyPlayer.Ins.MyBackpack.GetItem(ItemTag);
            }
            Destroy(gameObject);
        }

        private void OnBuffChange(Buff.ST_ItemBuffData[] buffDatas)
        {
            Buff.STBuffEffectData buffEffectData = new Buff.STBuffEffectData(buffDatas);
            listComponent?.ForEach((component) => component.OnBuffChange(buffEffectData));
        }

        public void OnPointerEnter(PointerEventData eventData)
        {
            bShowItemDes = true;

            if (uiDesItem == null)
                uiDesItem = UIDesCanvas.Ins.ShowDes(string.Empty, transform);
            else
                uiDesItem.Show(string.Empty, transform);
        }

        public void OnPointerExit(PointerEventData eventData)
        {
            bShowItemDes = false;
         
            if (uiDesItem != null)
            {
                uiDesItem.Hide();
                uiDesItem = null;
            }
        }

        protected void RefreshDesUI()
        {
            if (uiDesItem == null) return;

            sb.Clear();

            sb.Append(strItemName);
            sb.Append('\n');

            for (int i = 0; i < listComponent.Count; i++)
            {
                string des = listComponent[i].GetDes();
                if (string.IsNullOrEmpty(des)) continue;

                sb.Append(des);
                sb.Append('\n');
            }

            //去掉多余的 \n
            sb.Remove(sb.Length - 1, 1);

            uiDesItem.SetText(sb.ToString());
        }
    }
}